package com.zjcloud.jwgl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zjcloud.common.core.domain.entity.SysUser;
import com.zjcloud.common.utils.SecurityUtils;
import com.zjcloud.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zjcloud.common.annotation.Log;
import com.zjcloud.common.core.controller.BaseController;
import com.zjcloud.common.core.domain.AjaxResult;
import com.zjcloud.common.enums.BusinessType;
import com.zjcloud.jwgl.domain.AcTeacherInfo;
import com.zjcloud.jwgl.service.IAcTeacherInfoService;
import com.zjcloud.common.utils.poi.ExcelUtil;
import com.zjcloud.common.core.page.TableDataInfo;

/**
 * 教师信息Controller
 *
 * @author wxl
 * @date 2024-09-13
 */
@RestController
@RequestMapping("/jwgl/teacherinfo")
public class AcTeacherInfoController extends BaseController
{
    @Autowired
    private IAcTeacherInfoService acTeacherInfoService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询教师信息列表
     */
    @PreAuthorize("@ss.hasPermi('jwgl:teacherinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(AcTeacherInfo acTeacherInfo)
    {
        startPage();
        List<AcTeacherInfo> list = acTeacherInfoService.selectAcTeacherInfoList(acTeacherInfo);
        return getDataTable(list);
    }

    /**
     * 导出教师信息列表
     */
    @PreAuthorize("@ss.hasPermi('jwgl:teacherinfo:export')")
    @Log(title = "教师信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AcTeacherInfo acTeacherInfo)
    {
        List<AcTeacherInfo> list = acTeacherInfoService.selectAcTeacherInfoList(acTeacherInfo);
        ExcelUtil<AcTeacherInfo> util = new ExcelUtil<AcTeacherInfo>(AcTeacherInfo.class);
        util.exportExcel(response, list, "教师信息数据");
    }

    /**
     * 获取教师信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('jwgl:teacherinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(acTeacherInfoService.selectAcTeacherInfoById(id));
    }

    /**
     * 新增教师信息
     */
    @PreAuthorize("@ss.hasPermi('jwgl:teacherinfo:add')")
    @Log(title = "教师信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AcTeacherInfo acTeacherInfo)
    {

        SysUser sysUser = new SysUser();
        sysUser.setUserName(acTeacherInfo.getName());
        sysUser.setNickName(acTeacherInfo.getName());
        if (!userService.checkUserNameUnique(sysUser))
        {
            return error("新增用户'" + sysUser.getUserName() + "'失败，登录账号已存在");
        }
        // 初始密码 123456
        sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
        userService.insertUser(sysUser);

        // 获取用户id
        SysUser user = userService.selectUserByUserName(acTeacherInfo.getName());

        acTeacherInfo.setUserId(user.getUserId());

        return toAjax(acTeacherInfoService.insertAcTeacherInfo(acTeacherInfo));
    }

    /**
     * 修改教师信息
     */
    @PreAuthorize("@ss.hasPermi('jwgl:teacherinfo:edit')")
    @Log(title = "教师信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AcTeacherInfo acTeacherInfo)
    {
        return toAjax(acTeacherInfoService.updateAcTeacherInfo(acTeacherInfo));
    }

    /**
     * 删除教师信息
     */
    @PreAuthorize("@ss.hasPermi('jwgl:teacherinfo:remove')")
    @Log(title = "教师信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        if(ids.length > 0){
            for (Long id : ids) {
                userService.deleteUserById(id);
            }
        }
        return toAjax(acTeacherInfoService.deleteAcTeacherInfoByIds(ids));
    }
}
